package com.sun.umbrella.controller.res;


import com.sun.core.enums.BaseErrorEnum;
import com.sun.core.ex.BusinessProcessFailException;
import com.sun.core.web.result.Result;
import com.sun.umbrella.domain.bo.SysBO;
import com.sun.umbrella.domain.vo.ResUpdateVO;
import com.sun.umbrella.domain.vo.ResSaveVO;
import com.sun.umbrella.domain.bo.ResInfoBO;
import com.sun.umbrella.service.bns.res.ResInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.List;

/**
 * @description 资源基础信息接口实现类
 * @author: Jay
 * @date: 2018-04-09 16:30
 * @version: v1.0
 */

@Controller
@RequestMapping("/pc/res")
@Slf4j
public class ResInfoController {


    @Autowired
    private ResInfoService resInfoService;

    /**
     * 资源基础信息保存接口
     *
     * @param resSaveVO
     * @return
     */
    @RequestMapping("/saveRes")
    @ResponseBody
    public Result<Boolean> saveRes(ResSaveVO resSaveVO) {
        return Result.wrapSuccessfulResult(resInfoService.saveRes(resSaveVO));
    }

    /**
     * 资源基础信息删除接口
     *
     * @param id
     * @param sysId
     * @return
     */
    @RequestMapping("/deleteRes")
    @ResponseBody
    public Result<?> deleteRes(Integer sysId,Integer id) {
        //删除节点
        return Result.wrapSuccessfulResult(resInfoService.deleteRes(sysId, id));
    }

    /**
     * 根据id查询资源基础信息
     *
     * @param id
     * @param sysId
     * @return
     */
    @RequestMapping("/getResById")
    @ResponseBody
    public Result<ResInfoBO> getResById(Integer id, Integer sysId) {
        if (id == null) {
            throw new BusinessProcessFailException("id不能为空", BaseErrorEnum.BNS_CHK_ERROR.getCode());
        }
        if (sysId == null) {
            throw new BusinessProcessFailException("sysId不能为空", BaseErrorEnum.BNS_CHK_ERROR.getCode());
        }
        return Result.wrapSuccessfulResult(resInfoService.getResById(id, sysId));
    }

    /**
     * 资源基础信息查询接口
     *
     * @param sysId   系统Id
     * @param resName 资源名称
     * @return 返回查询结果
     */
    @RequestMapping("/queryResourceList")
    @ResponseBody
    public Result<List<ResInfoBO>> queryRessourceList(String resName, Integer sysId) {
        // 返回根节点的树结构
        return Result.wrapSuccessfulResult(resInfoService.queryRessourceList(resName, sysId));
    }


    /**
     * 根据roleId和sysId查询资源基础信息
     *
     * @param sysId   系统Id
     * @param roleId 角色id
     * @return 返回查询结果
     */
    @RequestMapping("/queryUserRes")
    @ResponseBody
    public Result<List<ResInfoBO>> queryRessourceListByRoleIdAndSysId(Integer roleId, Integer sysId) {
        // 返回根节点的树结构
        ResInfoBO resInfoBO = resInfoService.queryRessourceListByRoleIdAndSysId(roleId, sysId);
        if (resInfoBO == null){
            return Result.wrapSuccessfulResult(Arrays.<ResInfoBO>asList());
        }
        return Result.wrapSuccessfulResult(Arrays.asList(resInfoBO));
    }

    /**
     * 资源基础信息修改接口(可能会动节点位置)
     *
     * @param resUpdateVO 要修改的资源信息
     *                    <p>
     *                    如果传入的fatherId为空，则不需要移动节点位置
     *                    如果传入的fatherId不为空，则需要移动节点位置，
     *                    如果传入的brotherId不为空，则移动到brotherId的右边
     *                    如果传入的brotherId为空，则移动到fatherId的最左边
     *                    </p>
     * @return
     */
    @RequestMapping("/updateResource")
    @ResponseBody
    public Result<Boolean> updateResource(ResUpdateVO resUpdateVO) {
        return Result.wrapSuccessfulResult(resInfoService.updateResource(resUpdateVO));
    }

    /**
     * 获取系统枚举
     *
     * @return
     */
    @RequestMapping("/sysEnum")
    @ResponseBody
    public Result<List<SysBO>> querySysList() {
        return Result.wrapSuccessfulResult(resInfoService.querySysList());
    }


    /*=====================================================private method================================================================*/


}
